home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / Radio / radio / radio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-08  |  3.4 KB  |  167 lines

  1. #include "gopher.h"
  2.  
  3.  
  4. /*
  5.  * Connect_To_Gopher Performs a connecting to socket 'service' on host
  6.  * 'host'.  Host can be a hostname or ip-address.  If 'host' is null, the
  7.  * local host is assumed.   The parameter full_hostname will, on return,
  8.  * contain the expanded hostname (if possible).  Note that full_hostname is a
  9.  * pointer to a char *, and is allocated by connect_to_gopher()
  10.  *
  11.  * Errors:
  12.  *
  13.  * -1 get service failed
  14.  *
  15.  * -2 get host failed
  16.  *
  17.  * -3 socket call failed
  18.  *
  19.  * -4 connect call failed
  20.  */
  21.  
  22. int connect_to_gopher(host)
  23.   char *host;
  24. {
  25.      int s, service;
  26.      char  buf[MAXSTR];
  27.      struct sockaddr_in server;
  28.      struct hostent *hp;
  29.  
  30.      service = 7000;
  31.      
  32.      if (host == '\0') {
  33.       gethostname(buf, sizeof(buf));
  34.       host = buf;
  35.      }
  36.  
  37.      if ((server.sin_addr.s_addr = inet_addr(host)) == -1) {
  38.       if (hp = gethostbyname(host)) {
  39.            bzero((char *) &server, sizeof(server));
  40.            bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
  41.            server.sin_family = hp->h_addrtype;
  42.       } else
  43.            return (-2);
  44.      } else
  45.       server.sin_family = AF_INET;
  46.  
  47.      server.sin_port = (unsigned short) htons(service);
  48.  
  49.      if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  50.       return (-3);
  51.  
  52.      setsockopt(s, SOL_SOCKET, ~SO_LINGER, 0, 0);
  53.      setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  54.      setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
  55.      if (connect(s, &server, sizeof(server)) < 0) {
  56.       close(s);
  57.       return (-4);
  58.      }
  59.      return s;
  60. }
  61.  
  62.  
  63.  
  64. void check_sock(sockfd, iNum)
  65.   int sockfd, iNum;
  66. {
  67.      char DispString[80];
  68.      char Response[40];
  69.  
  70.      Response[0] = '\0';
  71.      
  72.      switch(sockfd)  {
  73.      case -4:
  74.       fprintf(stderr, "connect call failed ");
  75.       break;
  76.      case -3:
  77.       (void)fprintf(stderr, "socket call failed");
  78.       break;
  79.      case -2:
  80.       (void)fprintf(stderr, "get host failed");
  81.       break;
  82.      case -1:
  83.       (void)fprintf(stderr, "get service failed");
  84.       break;
  85.      }
  86.      fprintf(stderr, "to host %s, port %d\n",
  87.          Gopher[iNum].sHost, Gopher[iNum].iPort);
  88.      exit(-1);
  89.      
  90. }
  91.  
  92.  
  93.  
  94.  
  95. char Playcmd[80];
  96.  
  97.  
  98.  
  99. main(argc, argv)
  100.   int argc;
  101.   char *argv[];
  102. {
  103.      int sockfd, length, i;
  104.      char inputline[512];
  105.  
  106.      strcpy(Playcmd, "play - ");
  107.      
  108.      /*** add any extra command line params to the play command ***/
  109.  
  110.      if (argc > 2) {
  111.       for (i= 3; i <= argc; i++)
  112.            strcat(Playcmd, argv[i+1]);
  113.      }
  114.  
  115.      if ((sockfd = connect_to_gopher(argv[1], atoi(argv[2])) ) < 0) {
  116.       check_sock(sockfd, argv[1]);
  117.       exit(-1);
  118.      }
  119.  
  120.      /*** Send off a bogus line to start up the radio. ***/
  121.  
  122.      writestring(sockfd, "How's about some sound?\r\n");
  123.  
  124. /*     length = readline(sockfd, inputline, 512);
  125.      if (inputline[0]=='-') {
  126.       fprintf(stderr, "%s\n", inputline + 2);
  127.           return;
  128.      }
  129. */
  130.      
  131.      suck_sound(sockfd);
  132.       
  133. }
  134.  
  135.  
  136. /*
  137.  * this procedure just retrieves binary data from the socket and
  138.  * pumps it into a "play" process.
  139.  */
  140.  
  141. #define BUFSIZE 1400  /* A pretty good value for ethernet */
  142.  
  143. suck_sound(sockfd)
  144.   int sockfd;
  145. {
  146.      FILE *Play;
  147.      int j;
  148.      char buf[BUFSIZE];
  149.  
  150.      if (Playcmd[0] == '\0')
  151.           /*** Hey! no play command, bummer ***/
  152.           /*** TODO, notify user that there isn't a play command, damn.***/
  153.           return(0);
  154.  
  155.      Play = popen(Playcmd, "w");
  156.  
  157.      while(1) {
  158.           j = read(sockfd, buf, BUFSIZE);
  159.  
  160.           if (j == 0)
  161.                break;
  162.  
  163.           fwrite(buf, 1, j, Play);
  164.      }
  165. }
  166.  
  167.